package com.ruoyi.system.WebController;

import cn.hutool.core.date.DateTime;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysMenu;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginBody;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.ip.IpUtils;
import com.ruoyi.framework.web.service.SysLoginService;
import com.ruoyi.framework.web.service.SysPermissionService;
import com.ruoyi.system.domain.UserLoginInfo;
import com.ruoyi.system.service.ISysMenuService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.service.IUserLoginInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;


@RestController
@RequestMapping("/byWeb/qUser")
public class WebLoginController extends BaseController {

    @Autowired
    private SysLoginService loginService;

    @Autowired
    private ISysMenuService menuService;

    @Autowired
    private SysPermissionService permissionService;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private IUserLoginInfoService userLoginInfoService;

    /**
     * 登录方法
     *
     * @param loginBody 登录信息
     * @return 结果
     */
    @PostMapping("/qLogin")
    public AjaxResult login(@RequestBody LoginBody loginBody)
    {
        loginService.validateCaptcha(loginBody.getUsername(),loginBody.getCode(),loginBody.getUuid());
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        String token = loginService.qLogin(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(),
                loginBody.getUuid());
        ajax.put(Constants.TOKEN, token);
        UserLoginInfo userLoginInfo = new UserLoginInfo();
        userLoginInfo.setLoginUser("");
        userLoginInfo.setIp(IpUtils.getIpAddr());
        userLoginInfo.setRemark(token);
        userLoginInfo.setCreateTime(DateTime.now());
        userLoginInfo.setUsernNme(loginBody.getUsername());
        userLoginInfoService.insertUserLoginInfo(userLoginInfo);

        return ajax;
    }


    /**
     * 注册方法
     *
     * @param
     * @return 结果
     */
    @PostMapping("/register")
    public AjaxResult register(@Validated @RequestBody SysUser user)
    {
        //注册判断验证码
        loginService.validateCaptcha(user.getUserName(),user.getCode(),user.getUuid());
        if (!userService.checkUserNameUnique(user))
        {
            return error("新增用户'" + user.getUserName() + "'失败，登录账号已存在");
        }
        else if (StringUtils.isNotEmpty(user.getPhonenumber()) && !userService.checkPhoneUnique(user))
        {
            return error("新增用户'" + user.getUserName() + "'失败，手机号码已存在");
        }
        else if (StringUtils.isNotEmpty(user.getEmail()) && !userService.checkEmailUnique(user))
        {
            return error("新增用户'" + user.getUserName() + "'失败，邮箱账号已存在");
        }
//        user.setCreateBy(getUsername());
        user.setNickName(user.getUserName());
        user.setPassword(SecurityUtils.encryptPassword(user.getPassword()));
        return toAjax(userService.insertUser(user));
    }


    /**
     * 获取用户信息
     *
     * @return 用户信息
     */
    @GetMapping("getInfo")
    public AjaxResult getInfo()
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        // 角色集合
        Set<String> roles = permissionService.getRolePermission(user);
        // 权限集合
        Set<String> permissions = permissionService.getMenuPermission(user);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("user", user);
        ajax.put("roles", roles);
        ajax.put("permissions", permissions);
        return ajax;
    }

}
